package com.cn.lp.jpa.cq.test.domain;

import org.quartz.impl.triggers.CronTriggerImpl;

import java.text.ParseException;
import java.util.Date;
import java.util.Optional;

/**
 * cp每日任务触发器
 */
public class CPGMGainWinningNumTrigger implements TaskTrigger {

    private String gameCode;

    private String phaseNo;

    private CronTriggerImpl cronTrigger;

    public static CPGMGainWinningNumTrigger of(String gameCode, String phaseNo, long startTime, Long previousTime, Long nextTime,
        Long endTime, String cronExpression) {
        try {
            CPGMGainWinningNumTrigger trigger = new CPGMGainWinningNumTrigger();
            CronTriggerImpl cronTrigger = new CronTriggerImpl();
            cronTrigger.setCronExpression(cronExpression);
            cronTrigger.setStartTime(new Date(startTime));
            cronTrigger.setEndTime(endTime != null ? new Date(endTime) : null);
            cronTrigger.setPreviousFireTime(previousTime != null ? new Date(previousTime) : null);
            cronTrigger.setNextFireTime(nextTime != null ? new Date(nextTime) : null);
            trigger.cronTrigger = cronTrigger;
            trigger.gameCode = gameCode;
            trigger.phaseNo = phaseNo;
            return trigger;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getGameCode() {
        return gameCode;
    }

    public static CPGMGainWinningNumTrigger startAt(String gameCode, String phaseNo, long startTime,
        Long endTime, String cronExpression) {
        try {
            CPGMGainWinningNumTrigger trigger = new CPGMGainWinningNumTrigger();
            CronTriggerImpl cronTrigger = new CronTriggerImpl();
            cronTrigger.setCronExpression(cronExpression);
            cronTrigger.setStartTime(new Date(startTime));
            cronTrigger.setEndTime(endTime != null ? new Date(endTime) : null);
            cronTrigger.setPreviousFireTime(null);
            trigger.cronTrigger = cronTrigger;
            trigger.gameCode = gameCode;
            trigger.phaseNo = phaseNo;
            trigger.trigger(startTime);
            return trigger;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void beEnd() {
        cronTrigger.setEndTime(new Date());
    }

    public String getPhaseNo() {
        return phaseNo;
    }

    @Override
    public Optional<Long> getTriggerTimeAfter(long currentTime) {
        Date date = cronTrigger.getFireTimeAfter(new Date(currentTime));
        if (date == null) {
            return Optional.empty();
        } else {
            return Optional.of(date.getTime());
        }
    }

    @Override
    public Optional<Long> getNextTriggerTime() {
        Date date = cronTrigger.getNextFireTime();
        if (date == null) {
            return Optional.empty();
        } else {
            return Optional.of(date.getTime());
        }
    }

    @Override
    public long getStartTime() {
        return cronTrigger.getStartTime().getTime();
    }

    @Override
    public Optional<Long> getEndTime() {
        Date date = cronTrigger.getEndTime();
        if (date == null) {
            return Optional.empty();
        } else {
            return Optional.of(date.getTime());
        }
    }

    @Override
    public Optional<Long> getPreviousTriggerTime() {
        Date date = cronTrigger.getPreviousFireTime();
        if (date == null) {
            return Optional.empty();
        } else {
            return Optional.of(date.getTime());
        }
    }

    @Override
    public void trigger() {
        cronTrigger.triggered(null);
    }

    @Override
    public void trigger(long executeTime) {
        cronTrigger.setNextFireTime(new Date(executeTime));
        cronTrigger.triggered(null);
    }

    //
//    public void start(CPTriggerData triggerData) {
//        this.triggerData = triggerData;
//        this.trigger();
//    }
//
//    public void goOn(CPTriggerData triggerData) {
//        this.triggerData = triggerData;
//    }
//
//    @Override
//    public Long getFireTimeAfter(long currentTime) {
//        long executeTime = currentTime;
//        while (true) {
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTimeInMillis(executeTime);
//            for (Long time : timeList) {
//                Calendar itemCal = Calendar.getInstance();
//                itemCal.setTimeInMillis(time);
//                calendar.set(Calendar.HOUR_OF_DAY, itemCal.get(Calendar.HOUR_OF_DAY));
//                calendar.set(Calendar.MINUTE, itemCal.get(Calendar.MINUTE));
//                calendar.set(Calendar.SECOND, 0);
//                calendar.set(Calendar.MILLISECOND, 0);
//                if (this.getEndTime() != null && calendar.getTimeInMillis() >= this.getEndTime()) {
//                    return null;
//                }
//                if (calendar.getTimeInMillis() >= executeTime) {
//                    return calendar.getTimeInMillis();
//                }
//            }
//            calendar.add(Calendar.DAY_OF_YEAR, 1);
//            executeTime = calendar.getTimeInMillis();
//        }
//    }
//
//    @Override
//    public Long getNextTime() {
//        return this.triggerData.getNextTriggerTime().orElse(null);
//    }
//
//    @Override
//    public Long getStartTime() {
//        return this.triggerData.getStartTime().orElse(null);
//    }
//
//    @Override
//    public Long getEndTime() {
//        return this.triggerData.getEndTime().orElse(null);
//    }
//
//    @Override
//    public Long getPreviousFireTime() {
//        return this.triggerData.getPreviousTriggerTime().orElse(null);
//    }
//
//    @Override
//    public void trigger() {
//        if (this.triggerData.getNextTriggerTime().isPresent()) {
//            this.triggerData.setPreviousFireTime(this.triggerData.getNextTriggerTime().get());
//            this.triggerData.setNextFireTime(this.getFireTimeAfter(this.triggerData.getNextTriggerTime().get()));
//            saveDate();
//        }
//    }
//
//    @Override
//    public void trigger(long executeTime) {
//        this.triggerData.setNextFireTime(executeTime);
//        if (this.triggerData.getNextTriggerTime().isPresent()) {
//            this.triggerData.setPreviousFireTime(this.triggerData.getNextTriggerTime().get());
//            this.triggerData.setNextFireTime(this.getFireTimeAfter(this.triggerData.getNextTriggerTime().get()));
//            saveDate();
//        }
//    }
//
//    private void saveDate() {
//        triggerDateDAO.save(this.triggerData);
//    }

}
